package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.pojo.dto.SignInSetDetailDTO;
import com.xbongbong.paas.pojo.dto.SignInSetUpdateDTO;
import com.xbongbong.paas.pojo.vo.SignInSetVO;
import com.xbongbong.paas.service.SignInSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.OtherSetEnum;
import com.xbongbong.pro.log.pojo.DetailLogPojo;
import com.xbongbong.saas.domain.entity.SignInSetEntity;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.SignInSetEnum;
import com.xbongbong.saas.model.SignInSetModel;
import com.xbongbong.saas.service.toolbox.help.LogHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 签到设置接口
 *
 * @author youli.chen
 * @version v1.0
 * @date 2019/1/9 20:12
 * @since v1.0
 */
@Service("signInSetService")
public class SignInSetServiceImpl implements SignInSetService {

    private static final Logger LOG = LoggerFactory.getLogger(SignInSetServiceImpl.class);

    @Resource
    private SignInSetModel signInSetModel;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private LogHelp logHelp;

    /**
     * 获取签到设置
     *
     * @param signInSetDetailDTO 获取签到设置入参
     * @return SignInSetVO
     * @throws XbbException 业务异常
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public XbbResponse<SignInSetVO> getSignInSet(SignInSetDetailDTO signInSetDetailDTO) throws XbbException {
        //TODO 获取签到设置是否开启的信息
        SignInSetEntity entity;
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", signInSetDetailDTO.getCorpid());
        param.put("del", DelEnum.NORMAL.getDel());
        List<SignInSetEntity> signInSetEntityList = signInSetModel.findEntitys(param);
        if (signInSetEntityList.size() > 0) {
            entity = signInSetEntityList.get(0);
        } else {
            // 获取默认签到设置配置
            entity = new SignInSetEntity(signInSetDetailDTO.getCorpid(), 0, SignInSetEnum.AdjustType.ADJUST_TYPE_100.getCode(),
                    1, 0, SignInSetEnum.VisibleDistance.VISIBLE_DISTANCE_3000.getCode(), 0, 0, 1);
            // 默认配置入库
            signInSetModel.insert(entity);
        }

        SignInSetVO signInSetVO = new SignInSetVO();
        BeanUtils.copyProperties(entity, signInSetVO);
        // 对象赋值
        signInSetVO.setAdjustTypeShow(SignInSetEnum.AdjustType.getAdjustByCode(entity.getAdjustType()).getName());
        signInSetVO.setAdjustTypeValue(SignInSetEnum.AdjustType.getAdjustByCode(entity.getAdjustType()).getAdjustDistance());
        signInSetVO.setVisibleCustomerDistanceShow(SignInSetEnum.VisibleDistance.getVisibleDistanceByCode
                (entity.getVisibleCustomerDistance()).getName());
        signInSetVO.setVisibleCustomerDistanceValue(SignInSetEnum.VisibleDistance.getVisibleDistanceByCode
                (entity.getVisibleCustomerDistance()).getDistance());
        signInSetVO.setSignInId(entity.getId());
        return new XbbResponse<>(signInSetVO);
    }

    /**
     * 更新签到设置
     *
     * @param signInSetUpdateDTO 更新签到入参
     * @return SignInSetVO
     * @throws XbbException 业务异常
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public XbbResponse<SignInSetVO> updateSignInSet(SignInSetUpdateDTO signInSetUpdateDTO) throws XbbException {
        Long id = signInSetUpdateDTO.getSignInId();
        SignInSetEntity entity = signInSetModel.getByKey(id, signInSetUpdateDTO.getCorpid());
        if (entity == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100404, SystemErrorCodeEnum.API_ERROR_100404.getMsg());
        }
        SignInSetEntity signInSetEntity = new SignInSetEntity();
        BeanUtil.copyProperties(entity, signInSetEntity, true);
        BeanUtil.copyProperties(signInSetUpdateDTO, signInSetEntity, true);
        signInSetEntity.setId(id);
        signInSetEntity.setUpdateTime(DateUtil.getInt());
        signInSetModel.update(signInSetEntity);

        // 记录日志
        String corpid = signInSetUpdateDTO.getCorpid();
        String userId = signInSetUpdateDTO.getUserId();
        // 登录用户名
        String loginUserName = signInSetUpdateDTO.getLoginUserName();

        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.AGG_MEMO_UPDATE_SIGN_SET), loginUserName);
        JSONObject oldObj = JsonHelperUtil.parseObject(JSONObject.toJSONString(entity));
        JSONObject newObj = JsonHelperUtil.parseObject(JSONObject.toJSONString(signInSetEntity));
        List<DetailLogPojo> detailArr = logHelp.getDetailArr4Normal(corpid, OperateModuleTypeEnum.SIGNIN, oldObj, newObj);
        mongoLogHelp.buildLog4DetailArr(detailArr, corpid, userId, loginUserName, OperateModuleTypeEnum.SIGNIN, OperateTypeEnum.EDIT, id.toString(), OtherSetEnum.CHECK_IN_SETTINGS.getName(), memo, signInSetUpdateDTO.getHttpHeader());

        SignInSetVO signInSetVO = new SignInSetVO();
        BeanUtils.copyProperties(signInSetEntity, signInSetVO);
        // 对象赋值
        signInSetVO.setAdjustTypeShow(SignInSetEnum.AdjustType.getAdjustByCode(signInSetEntity.getAdjustType()).getName());
        signInSetVO.setVisibleCustomerDistanceShow(SignInSetEnum.VisibleDistance.getVisibleDistanceByCode
                (signInSetEntity.getVisibleCustomerDistance()).getName());
        signInSetVO.setSignInId(id);
        return new XbbResponse<>(signInSetVO);
    }
}
